package ru.ys.jsolver.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import ru.ys.jsolver.interfaces.ISolveListener;
import ru.ys.jsolver.model.Board;
import ru.ys.jsolver.model.ForbiddenSittuationException;
import ru.ys.jsolver.model.Board.LineType;

import Tasks.FromNatasha;

public class CrosswordEditor extends JFrame{
	Board myBoard;
	int myWidth;
	int myHeight;
	MyTable myColTable;
	MyTable myRowTable;
	BoardComponent myBoardComponent;
	
	JMenu myFileMenu;
	JMenu myEditMenu;
	JMenu mySolveMenu;
	
	private void onNewSelected() {
		System.out.println("New");		
	}
	
	private void onOpenSelected() {
		System.out.println("Open");
	}
	
	private void onSaveSelected() {
		System.out.println("Save");
	}
	
	private void onExitSelected() {
		// TODO add request
		dispose();
	}
	
	private void onAddRowSelected() {
		System.out.println("add row");		
	}
	
	private void onAddColSelected() {
		System.out.println("add col");
	}
	
	private void onRmRowSelected() {
		System.out.println("rm row");
	}
	
	private void onRmColSelected() {
		System.out.println("rm col");
	}
	
	
	
	
	private void makeFileMenu() {
		JMenuItem menuNew = new JMenuItem("New");
		menuNew.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				onNewSelected();				
			}});		
		myFileMenu.add(menuNew);
		
		JMenuItem menuOpen = new JMenuItem("Open");
		menuOpen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				onOpenSelected();				
			}});		
		myFileMenu.add(menuOpen);
		
		JMenuItem menuSave = new JMenuItem("Save");
		menuSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				onSaveSelected();				
			}});		
		myFileMenu.add(menuSave);
		
		JMenuItem menuExit = new JMenuItem("Exit");
		menuExit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				onExitSelected();				
			}});		
		myFileMenu.add(menuExit);				
	}
	
	private void makeEditMenu() {
		{
			JMenuItem i = new JMenuItem("Add row");
			i.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onAddRowSelected();					
				}});
			myEditMenu.add(i);
		}
		{
			JMenuItem i = new JMenuItem("Add collumn");
			i.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onAddColSelected();					
				}});
			myEditMenu.add(i);
		}
		{
			JMenuItem i = new JMenuItem("Remove row");
			i.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onRmRowSelected();					
				}});
			myEditMenu.add(i);
		}
		{
			JMenuItem i = new JMenuItem("Remove collumn");
			i.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onRmColSelected();					
				}});
			myEditMenu.add(i);
		}				
	}
	
	private void makeSolveMenu() {
		mySolveMenu.add(new JMenuItem("Solve"));
		mySolveMenu.add(new JMenuItem("Make supposition"));
		mySolveMenu.add(new JMenuItem("Remove supposition"));
		
	}
	
	private void makeMenu() {		
		JMenuBar myMenu = new JMenuBar(); 
		add(myMenu);	
		myFileMenu = new JMenu("File");
		myEditMenu = new JMenu("Edit");
		mySolveMenu = new JMenu("Solve"); 
		
		myMenu.add(myFileMenu);
		myMenu.add(myEditMenu);
		myMenu.add(mySolveMenu);
		myMenu.add(Box.createHorizontalGlue());
		makeFileMenu();
		makeEditMenu();
		makeSolveMenu();
		
	}
	
	private void setFirstLevel() {
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		makeMenu();
		add(Box.createVerticalStrut(12));
		JPanel firstLevelPanel = new JPanel();
		add(firstLevelPanel);
		setSecondLevel(firstLevelPanel);
	}
	
	private void setSecondLevel(JPanel c) {
		BoxLayout secondLevelLayout = new BoxLayout(c, BoxLayout.X_AXIS);		
		c.setLayout(secondLevelLayout);
		
		JPanel leftPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		
		c.add(leftPanel);
		c.add(Box.createHorizontalStrut(5));
		c.add(rightPanel);
		
		setLeftPanel(leftPanel);
		setRightPanel(rightPanel);
		
				
	}
	
	private void setLeftPanel(JPanel c) {
		BoxLayout leftLayout = new BoxLayout(c, BoxLayout.Y_AXIS);		
		c.setLayout(leftLayout);
		
		myRowTable = new MyTable(myHeight, 1); 
		c.add(Box.createVerticalGlue());
		c.add(myRowTable);		
		
	}
	
	private void setRightPanel(JPanel c) {		
		BoxLayout rightLayout = new BoxLayout(c, BoxLayout.Y_AXIS);		
		c.setLayout(rightLayout);
		
		myColTable = new MyTable(1,myWidth); 
		c.add(myColTable);		
		c.add(Box.createVerticalStrut(5));		
		myBoard = new Board(new FromNatasha());
		myBoardComponent = new BoardComponent(myBoard); 
		c.add(myBoardComponent);		
	}
	
	private void setSizeOfTables() {
		int cellSize = (int)(myBoardComponent.getPreferredSize().getWidth() / myColTable.getColumnCount()); 
		myColTable.setCellSize(cellSize);
		myRowTable.setCellSize(cellSize);	
	}
	
	public CrosswordEditor(int x, int y) {
		setTitle("Solver");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		myWidth = x;
		myHeight = y;
		setFirstLevel();
		setSizeOfTables();
		
		try {
			myBoard.solve(new ISolveListener() {
				@Override
				public void sittuationChanged(LineType lineType, int index) {
					repaint();				
				}});
		} catch (ForbiddenSittuationException e) {			
			e.printStackTrace();
		}
		
		
		
		
		
		
		pack();
		setVisible(true);
	}
}
